package com.htht.smartschool.listener;

import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

import redis.clients.jedis.ShardedJedis;

import com.htht.smartschool.common.dao.DataBaseManager;
import com.htht.smartschool.common.job.DownFileJob;
import com.htht.smartschool.common.job.UpFileJob;
import com.htht.smartschool.util.message.MessageUtil;
import com.htht.smartschool.util.redis.RedisUtil;
import com.htht.smartschool.util.system.ConfigUtil;
import com.htht.smartschool.util.system.ImgUtil;
import com.htht.smartschool.util.system.ResultSetToExcel;

public class StartupListener implements ServletContextListener {
	public StartupListener() {
	}

	@Override
	public void contextDestroyed(ServletContextEvent sce) {

	}

	@Override
	public void contextInitialized(ServletContextEvent sce) {
		DataBaseManager.getInst();
		this.doStartRedis();
		ImgUtil.usersimgpath = sce.getServletContext().getRealPath(ConfigUtil.getProperty("default","usersfile"));
		ImgUtil.studentsimgpath = sce.getServletContext().getRealPath(ConfigUtil.getProperty("default","studentsfile"));
		ResultSetToExcel.savepath = sce.getServletContext().getRealPath(ConfigUtil.getProperty("downloads","url"));
	}
	/**
	 * 启动redis
	 */
	private void doStartRedis(){
		initApp();
		ShardedJedis jedis = RedisUtil.getInstance().jedis;
		if(jedis == null){
			RedisUtil.getInstance().setConfigRedis(ConfigUtil.getProperty("redis","host"),Integer.parseInt(ConfigUtil.getProperty("redis","port")),ConfigUtil.getProperty("redis","pass"));
		}
	}
	@SuppressWarnings("static-access")
	public void initApp(){
		MessageUtil.appids.put("teacherid",Integer.parseInt(ConfigUtil.getProperty("message","teacherid")));
		MessageUtil.appids.put("userid",Integer.parseInt(ConfigUtil.getProperty("message","userid")));
		try {
			InetAddress ia=null;
			ia=ia.getLocalHost();
			SchedulerFactory factory = new StdSchedulerFactory(); 
			Scheduler scheduler = factory.getScheduler();
			JobDetail upjob = JobBuilder.newJob(UpFileJob.class).withIdentity("upjob", "group1").build();  
		    CronTrigger ctup = TriggerBuilder.newTrigger().withIdentity("upjob", "group1")  
		                .withSchedule(CronScheduleBuilder.cronSchedule(ConfigUtil.getProperty("ftp","uptime"))).build();  
		    JobDetail downjob = JobBuilder.newJob(DownFileJob.class).withIdentity("downjob", "group1").build();  
		    CronTrigger ctdown = TriggerBuilder.newTrigger().withIdentity("downjob", "group1")  
		                .withSchedule(CronScheduleBuilder.cronSchedule(ConfigUtil.getProperty("ftp","downtime"))).build();
		    scheduler.scheduleJob(upjob, ctup);  
		    scheduler.scheduleJob(downjob, ctdown);  
		    scheduler.start();  
		} catch (SchedulerException e) {
			e.printStackTrace();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}  
  
	}
}
